package com.rayboo.service;

import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rayboo.model.ClubUser;
import com.rayboo.repository.ClubUserRepository;

import javafx.util.Pair;

/**
 * 
 * ClubUserService
 * 
 * @author ray
 * @date 2016年9月8日 下午8:11:05
 *
 */
@SuppressWarnings("restriction")
@Service
public class ClubUserService {

	private static final org.slf4j.Logger log = LoggerFactory.getLogger(ClubUserService.class);

	@Autowired
	ClubUserRepository clubUserRepository;

	/**
	 * 获取条数和列表
	 * 
	 * @param pageable
	 * @return
	 */
	public Pair<Integer, List<ClubUser>> find(Pageable pageable) {
		Long total = null;
		List<ClubUser> clubusers = null;
		try {
			total = clubUserRepository.count();
			clubusers = clubUserRepository.findAll(pageable);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Pair<Integer, List<ClubUser>>(total.intValue(), clubusers);
	}

	/**
	 * 获取总数
	 * 
	 * @return
	 */
	public Integer count() {
		Integer total = (int) clubUserRepository.count();
		return total;
	}

	/**
	 * 根据条件获取列表（支持分页和模糊查询）
	 * 
	 * @param query
	 * @param pageable
	 * @return
	 */
	public List<ClubUser> find(String query, Pageable pageable) {
		List<ClubUser> clubusers = null;
		try {
			clubusers = clubUserRepository.find(query, pageable);
			log.info("clubuser list:" + clubusers);
		} catch (Exception e) {
			log.error("find exception", e);
		}
		return clubusers;
	}

	/**
	 * 获取记录数（支持模糊查询）
	 * 
	 * @return
	 */
	public Integer count(String query) {
		Integer total = null;
		try {
			total = clubUserRepository.count(query);
			log.info("clubuser count:" + total);
		} catch (Exception e) {
			log.error("count exception", e);
		}
		return total;
	}

	/**
	 * 根据userId查询用户信息
	 * 
	 * @param userid
	 * @return
	 */
	public ClubUser findByUserId(Integer userid) {
		if (userid == 0)
			return null;
		return clubUserRepository.findByUserId(userid);
	}

	/**
	 * 根据userId更新VIP信息
	 * 
	 * @param userid
	 * @param vip
	 * @return
	 */
	@Transactional
	public ClubUser updateVIP(Integer userid, long vip) {
		// 根据userid获取用户信息
		ClubUser clubUser = findByUserId(userid);
		if (clubUser == null)
			return null;
		clubUser.setVip(vip);
		log.info("update clubuser.vip:" + clubUser.getVip());
		return clubUserRepository.save(clubUser);
	}

	/**
	 * 根据userId更新用户信息
	 * 
	 * @param userid
	 * @param clubUser
	 * @return
	 */
	@Transactional
	public ClubUser update(Integer userid, ClubUser clubUser) {
		// 根据userid获取用户信息
		ClubUser user = findByUserId(userid);
		if (user == null)
			return null;
		// TODO 其他字段更新
		user.setVip(clubUser.getVip());
		log.info("update clubuser:" + clubUser);
		return clubUserRepository.save(user);
	}

}
